package top.lrshuai.xml.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import top.lrshuai.xml.dao.DetailedMapper;
import top.lrshuai.xml.dao.UserMapper;
import top.lrshuai.xml.entity.User;

import java.util.*;

@RestController
public class UserController {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private DetailedMapper detailedMapper;

    @RequestMapping("/getAll")
    public Object getAllList() throws Exception {
        List<User> ulist = userMapper.getAll();
        System.out.println("ulist=" + ulist);
        return ulist;
    }


    /**
     * 这个{id}是restful 风格的请求
     *
     * @param
     * @return
     * @throws Exception
     */
    @RequestMapping("/user/{id}")
    public Object getUserById(@PathVariable("id") Long uid) throws Exception {
        System.out.println("uid" + uid);
        User u = userMapper.getUserById(uid);
        System.out.println("user=" + u);
        return u == null ? "没有这个用户" : u;
    }

    @RequestMapping("/save")
    public String saveUser() throws Exception {
        Map<String, Object> map = new HashMap<>();
        map.put("username", "userName" + UUID.randomUUID().toString().replaceAll("-", ""));
        map.put("nickName", "nick" + UUID.randomUUID().toString().replaceAll("-", ""));
        map.put("sex", new Random().nextInt(10) % 2 == 1 ? "man" : "woman");
        System.out.println("map=" + map);
        int rest = userMapper.save(map);
        return rest == 1 ? "成功" + map : "失败";
    }

    @RequestMapping("/update")
    public String updateUser() throws Exception {
        Map<String, Object> map = new HashMap<>();
        map.put("username", "userName" + UUID.randomUUID().toString().replaceAll("-", ""));
        map.put("nickName", "nick" + UUID.randomUUID().toString().replaceAll("-", ""));
        map.put("id", new Random().nextInt(5));
        System.out.println("map=" + map);
        int rest = userMapper.update(map);
        return rest == 1 ? "成功" + map : "失败";
    }

    @RequestMapping("/del/{id}")
    public String delUser(@PathVariable("id") Long id) throws Exception {
        int rest = userMapper.delete(id);
        return rest == 1 ? "成功" : "失败";
    }/**/

    //生成白卡
    @RequestMapping("/createwhitecard")
    public String createWhiteCard() throws Exception {
        //收到请求 ，查询库存
        int cardCount = detailedMapper.selectCardCount();
        System.out.println("count=" + cardCount);
        return "成功";
        // return "redirect:index.html"/* + cardCount*/;

       /* int startUser = 90000000;
        BufferedWriter bufferedWriter = null;
        Random random = new Random();
        String user = null;
        String password = null;
        int userLength = digit - Integer.toString(startUser).length();
        try {
            bufferedWriter = new BufferedWriter(new FileWriter(filePath));
            for (int i = 0; i < count; i++) {
                user = startUser + String.format("%0" + userLength + "d", maxNo);
                //密码等于账号 加 系统时间毫秒 加 99999之间的一个随机数 然后做MD5加密
                password = MD5Util.getMd5_16(user + System.currentTimeMillis() + random.nextInt(99999));

            }


        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                bufferedWriter.close();
            } catch (Exception e) {
                e.printStackTrace();
            }

        }*/

    }
}
